﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace BeeCoders.Tools.ControlCanvas.Infrastructure
{
	public static class SerializationHelper
	{
		static public void SerializeToXml(ControlCollection	controls,string fileName)
		{
			var serializer = new XmlSerializer(typeof(ControlCollection));
			TextWriter textWriter = new StreamWriter(fileName);
			serializer.Serialize(textWriter, controls);
			textWriter.Close();
		}

		static public ControlCollection DeserializeFromXml(string fileName)
		{
			var serializer = new XmlSerializer(typeof(ControlCollection));
			TextReader tr = new StreamReader(fileName);

			var cc = (ControlCollection)serializer.Deserialize(tr);
			tr.Close();

			return cc;
		}

		static public void SerializeToBinary(ControlCollection controls, string fileName)
		{
			var serializer = new BinaryFormatter();
			Stream stream = File.Open(fileName, FileMode.Create);
			serializer.Serialize(stream, controls);
			stream.Close();
		}

		static public ControlCollection DeserializeFromBinary(string fileName)
		{
			Stream stream = File.Open(fileName, FileMode.Open);
			var bformatter = new BinaryFormatter();

			var cc = (ControlCollection)bformatter.Deserialize(stream);
			stream.Close();
            
			return cc;
		}
	}
}
